using System;
using System.IO;
using System.Reflection;

namespace Needle.Utils
{

    /// <summary>
    /// Defines application specific folders.
    /// </summary>
    public static class AppFileSystem
    {

        static AppFileSystem()
        {

            AssemblyTitleAttribute[] titles;
            Assembly entryAssembly = Assembly.GetEntryAssembly();
            if (entryAssembly == null)
            {
                entryAssembly = Assembly.GetExecutingAssembly();
            }
            titles = Attributes.Get<AssemblyTitleAttribute>(entryAssembly);
            if (titles.Length > 0)
                m_AppName = titles[0].Title;
            else
                m_AppName = AppDomain.CurrentDomain.ApplicationIdentity.FullName;
        }

        private static readonly string m_AppName;
        /// <summary>
        /// The application name.
        /// </summary>
        public static string ApplicationName
        {
            get { return m_AppName; }
        }

        /// <summary>
        /// The current working folder
        /// </summary>
        public static DirectoryInfo CurrentFolder
        {
            get { return new DirectoryInfo(Environment.CurrentDirectory); }
        }
        /// <summary>
        /// The user's documents folder appended with the application name.
        /// </summary>
        public static DirectoryInfo UserDataFolder
        {
            get { return new DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), ApplicationName)); }
        }
        /// <summary>
        /// The startup executable folder.
        /// </summary>
        public static DirectoryInfo ExeFolder
        {
            get { return new DirectoryInfo(AppDomain.CurrentDomain.SetupInformation.ApplicationBase); }
        }
        /// <summary>
        /// The application configuration file. (for the current appdomain)
        /// </summary>
        public static FileInfo ConfigFile
        {
            get { return new FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); }
        }
        /// <summary>
        /// The application data folder appended with the application name that is shared between users.
        /// </summary>
        public static DirectoryInfo CommonDataFolder
        {
            get { return new DirectoryInfo(Path.Combine(FileSystem.CommonAppDataFolder.FullName, ApplicationName)); }
        }
        /// <summary>
        /// The local (non-roaming) user application data folder appended with the application name.
        /// </summary>
        public static DirectoryInfo LocalDataFolder
        {
            get { return new DirectoryInfo(Path.Combine(FileSystem.LocalAppDataFolder.FullName, ApplicationName)); }
        }
        /// <summary>
        /// The roaming user application data folder appended with the application name.
        /// </summary>
        public static DirectoryInfo DataFolder
        {
            get { return new DirectoryInfo(Path.Combine(FileSystem.AppDataFolder.FullName, ApplicationName)); }
        }
    }
}
